package com.ayuan.blog.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.expression.subexpression.ValueExpression;
import org.durcframework.core.service.CrudService;
import com.ayuan.blog.service.TagService;
import com.ayuan.blog.dao.TagDao;
import com.ayuan.blog.domain.Tag;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TagServiceImpl 
        extends CrudService<Tag, TagDao> 
        implements TagService {

    @Override
    public Tag addTag(Tag tag) {
        ExpressionQuery query = new ExpressionQuery();
        query.addValueExpression(new ValueExpression("tag_name", tag.getTagName()));
        List<Tag> tagList = find(query);
        if (CollectionUtils.isNotEmpty(tagList)) {
            tag = tagList.get(0);
        } else {
            tag = save(tag);
        }
        return tag;
    }

    @Override
    public List<Tag> findTagByArticleId(Integer articleid) {
        ExpressionQuery query = new ExpressionQuery();
        query.addValueExpression(new ValueExpression("at.article_id", articleid));
        return this.getDao().findTagByArticleId(query);
    }

    @Override
    public List<Tag> findAllTag() {
        ExpressionQuery query = new ExpressionQuery();
        query.setQueryAll(true);
        return this.getDao().find(query);
    }

    @Override
    public List<Tag> findListByIds(List<Integer> tagIdList) {
        return null;
    }

}